package cn.edu.cug.cs.gtl.oi.ui;

import cn.edu.cug.cs.gtl.oi.ui.menu.*;
import cn.edu.cug.cs.gtl.oi.ui.actions.*;

import javax.swing.*;
import java.awt.*;

public class MainWindow extends JFrame {

    MainApp theApp = null;
    JSplitPane splitPane = null;
    RenderPanel renderPanel = null;

    ProjectPanel projectPanel = null;

    TablePanel tablePanel = null;

    public MainWindow(MainApp theApp) throws HeadlessException {
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, GraphicsConfiguration gc) {
        super(gc);
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, String title) throws HeadlessException {
        super(title);
        this.theApp = theApp;
    }

    public MainWindow(MainApp theApp, String title, GraphicsConfiguration gc) {
        super(title, gc);
        this.theApp = theApp;
    }


    public RenderPanel getRenderPanel() {
        return renderPanel;
    }

    public ProjectPanel getProjectPanel() {
        return this.projectPanel;
    }

    public TablePanel getTablePanel() {
        return this.tablePanel;
    }

    public void initial() {
        initialMenus();
        initialRenderPanel();
        initialProjectPanel();
        initialTablePanel();

        getContentPane().setLayout(new BorderLayout());
        JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT, this.renderPanel, this.tablePanel);
        right.setDividerLocation(800);
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, projectPanel, right);
        splitPane.setEnabled(true);
        splitPane.setDividerLocation(300);
        setContentPane(splitPane);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    private void initialRenderPanel() {
        renderPanel = new RenderPanel(theApp);
        getRenderPanel().initial();
    }

    private void initialTablePanel() {
        tablePanel = new TablePanel(theApp);
        tablePanel.initial();
    }

    private void initialProjectPanel() {
        projectPanel = new ProjectPanel(theApp);
        projectPanel.initial();
    }

    private void initialMenus() {
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);

        // Menu
        JMenu fileMenu = new JMenu("File");

        JMenuItem openH3DFileItem = new JMenuItem("Open H3D File");

        openH3DFileItem.addActionListener(new OpenH3DFileActionListener(theApp));

        fileMenu.insert(openH3DFileItem, 0);

        JMenuItem openIvFileItem = new JMenuItem("Open IV File");

        openIvFileItem.addActionListener(new OpenIVFileActionListener(theApp));

        fileMenu.insert(openIvFileItem, 1);

        JMenuItem openOIVSItem = new JMenuItem("Run Open Inventor Studio");

        openOIVSItem.addActionListener(new OpenInventorStudioActionListener(theApp));

        fileMenu.insert(openOIVSItem, 2);
        fileMenu.insertSeparator(3);

        JMenuItem selectedItem = new JMenuItem("选择");
        selectedItem.addActionListener(new SelectActionListener(theApp));
        fileMenu.insert(selectedItem, 4);

        JMenuItem saveH3D = new JMenuItem("另存为...");
        saveH3D.addActionListener(new SaveH3DFileActionListener(theApp));
        fileMenu.insert(saveH3D, 5);

        JMenuItem appExitItem = new JMenuItem("Exit");

        appExitItem.addActionListener(new AppExitActionListener());
        fileMenu.insert(appExitItem, 6);

        JMenuBar menubar = new JMenuBar();
        menubar.add(fileMenu);
        menubar.add(new JMenuProject(theApp));
        menubar.add(new JMenuShow());
        menubar.add(new JMenuEdit());
        menubar.add(new JMenuGraphicEdit());
        menubar.add(new JMenuPointLine());
        menubar.add(new JMenuCurve());
        menubar.add(new JMenuEntity());
        menubar.add(new JMenuModeling());
        menubar.add(new JMenuAnalysis());
        menubar.add(new JMenuTool());
        menubar.add(new JMenuSystem());
        menubar.add(new JMenuHelp());

        this.setJMenuBar(menubar);
    }
}
